package com.jiuqi.abc.model.dao;

import com.jiuqi.abc.model.bean.DriverGroup;
import com.jiuqi.bi.util.OrderGenerator;
import com.jiuqi.bi.util.type.GUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//动因分组DAO
@Repository
public class ABCDriverGroupDao {

    //动因分组表
    private static final String T_TABLE = "ABC_M_DRIVER_GROUP";

    //表ID(主键)
    private static final String F_AMDG_ID = "AMDG_ID";

    //动因分组标题
    private static final String F_AMDG_TITLE = "AMDG_TITLE";

    //所属模型ID 与 分摊模型基本信息表 中 模型ID（主键） 关联
    private static final String F_AMM_ID = "AMM_ID";

    //动因分组顺序
    private static final String F_AMDG_ORDER = "AMDG_ORDER";

    @Autowired
    JdbcTemplate jdbcTemplate;


    //【增】批量新增 动因分组信息
    public void batchSaveDriverGroups(List<DriverGroup> driverGroups) {
        String addSql = String.format("INSERT INTO %s VALUES(?,?,?,?)", T_TABLE);
        List<Object[]> argList = new ArrayList<>();
        for (DriverGroup driverGroup : driverGroups) {

            if (Objects.isNull(driverGroup.getKey()) || ("").equals(driverGroup.getKey()))
                driverGroup.setKey(GUID.newGUID());

            Object[] args = {driverGroup.getKey(), driverGroup.getTitle(),
                             driverGroup.getBelongModelId(), OrderGenerator.newOrderID()};
            argList.add(args);
        }

        jdbcTemplate.batchUpdate(addSql, argList);
    }

    //【查】根据 动因分组ID 获取 动因分组
    public DriverGroup getDriverGroupByDriverGroupId(String driverGroupId){
        String selectSql = String.format("SELECT %s,%s,%s,%s FROM %s WHERE %s = ? ORDER BY %s",
                F_AMDG_ID, F_AMDG_TITLE, F_AMM_ID, F_AMDG_ORDER,
                T_TABLE,
                F_AMDG_ID,
                F_AMDG_ORDER);
        Object[] args = {driverGroupId};
        return jdbcTemplate.query(selectSql, rs -> {
            DriverGroup driverGroup = new DriverGroup();
            while (rs.next()) {
                try {
                    driverGroup.setKey(rs.getString(F_AMDG_ID));
                    driverGroup.setTitle(rs.getString(F_AMDG_TITLE));
                    driverGroup.setBelongModelId(rs.getString(F_AMM_ID));
                    driverGroup.setOrder(rs.getLong(F_AMDG_ORDER));
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            return driverGroup;
        }, args);
    }

    //【查】 根据 模型ID 获取 动因分组列表
    public List<DriverGroup> getDriverGroupListByModelId(String modelId) {
        String selectSql = String.format("SELECT %s,%s,%s,%s FROM %s WHERE %s = ? ORDER BY %s",
                F_AMDG_ID, F_AMDG_TITLE, F_AMM_ID, F_AMDG_ORDER,
                T_TABLE,
                F_AMM_ID,
                F_AMDG_ORDER);
        Object[] args = {modelId};
        return jdbcTemplate.query(selectSql, rs -> {
            List<DriverGroup> driverGroupList = new ArrayList<>();
            while (rs.next()) {
                try {
                    DriverGroup driverGroup = new DriverGroup();
                    driverGroup.setKey(rs.getString(F_AMDG_ID));
                    driverGroup.setTitle(rs.getString(F_AMDG_TITLE));
                    driverGroup.setBelongModelId(rs.getString(F_AMM_ID));
                    driverGroup.setOrder(rs.getLong(F_AMDG_ORDER));

                    driverGroupList.add(driverGroup);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            return driverGroupList;
        }, args);
    }

    //【改】 修改某一动因分组
    public void modifyDriverGroup(DriverGroup driverGroup) {
        String modifySql = String.format("UPDATE %s SET %s = ?,%s = ? WHERE %s = ?",
                T_TABLE,
                F_AMDG_TITLE,
                F_AMDG_ORDER,
                F_AMDG_ID);
        Object[] args = {driverGroup.getTitle(), driverGroup.getOrder(), driverGroup.getKey()};
        jdbcTemplate.update(modifySql, args);
    }

    //【删】 根据 动因分组ID 删除 某一动因分组
    public void deleteDriverGroupByDriverGroupId(String driverGroupId) {
        String deleteSql = String.format("DELETE FROM %s WHERE %s = ?", T_TABLE, F_AMDG_ID);
        Object[] args = {driverGroupId};
        jdbcTemplate.update(deleteSql, args);
    }

    //【删】 根据 模型ID 删除 多个动因分组
    public void deleteDriverGroupsByModelId(String modelId) {
        String deleteSql = String.format("DELETE FROM %s WHERE %s = ?", T_TABLE, F_AMM_ID);
        Object[] args = {modelId};
        jdbcTemplate.update(deleteSql, args);
    }

}
